package com.shj.em.biz.purchase.controller;

import com.shj.em.biz.company.entity.Company;
import com.shj.em.biz.company.service.CompanyService;
import com.shj.em.biz.purchase.dto.SearchPurchaseDTO;
import com.shj.em.biz.sys.entity.Cd;
import com.shj.em.biz.sys.service.CdService;
import org.springframework.stereotype.Controller;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Arrays;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.shj.em.common.annotation.Log;
import com.shj.em.base.support.ResMsg;
import com.shj.em.base.vo.PageVO;

import com.shj.em.biz.purchase.entity.Purchase;
import com.shj.em.biz.purchase.service.PurchaseService;
import com.baomidou.mybatisplus.plugins.Page;
import lombok.extern.slf4j.Slf4j;
import com.shj.em.base.controller.BaseController;


/**
 * <p>
 * 采购表 前端控制器
 * </p>
 *
 * @author Shen Huang Jian
 * @since 2018-11-04
 */
@Controller
@Slf4j
@RequestMapping("/purchase")
public class PurchaseController extends BaseController<Purchase>{

    private String prefix = "purchase/purchase";

    @Autowired
    PurchaseService purchaseService;

    @Autowired
    CompanyService companyService;

    @GetMapping("/gotoSearch")
    public String gotoSearch(Model model){
        addDropdown(model);
        return prefix + "/search";
    }

    private void addDropdown(Model model){
        List<Company> list = companyService.getAllSupplies();
        model.addAttribute("supplies", list);

        List<Cd> units = cdService.queryCdsByType(CdService.CD_TYPE_UNIT);
        model.addAttribute("units", units);
    }

    @RequestMapping("/list")
    @ResponseBody
    public PageVO<Purchase> list(@RequestBody SearchPurchaseDTO searchPurchaseDTO){
        Page<Purchase> page = purchaseService.queryPurchasePage(searchPurchaseDTO);
        PageVO<Purchase> pageVO = new PageVO<>();
        pageVO.setTotal(page.getTotal());
        pageVO.setRows(page.getRecords());
        return pageVO;
    }

    @GetMapping("/gotoAdd")
	String gotoAdd(Model model){
        addDropdown(model);
        Purchase purchase = new Purchase();
        purchase.setPurchaseDt(new Date());
        purchase.setUnit("G");
        model.addAttribute("isEdit", false);
        model.addAttribute("purchase", purchase);
	    return prefix + "/add";
	}

	@GetMapping("/gotoEdit/{id}")
	String gotoEdit(@PathVariable("id") Long id, Model model){
        Purchase purchase = purchaseService.selectById(id);
		model.addAttribute("purchase", purchase);
        model.addAttribute("isEdit", true);
	    return prefix + "/add";
	}

	@Log("创建采购表")
	@ResponseBody
	@PostMapping("/add")
	public ResMsg add(Purchase purchase){
        try{
            purchaseService.insert(purchase);
            return ResMsg.ok(new Purchase());
        }catch(Exception e){
            log.error("创建采购表失败", e);
            return ResMsg.error("创建采购表失败");
        }

	}

    @Log("更新采购表")
	@ResponseBody
	@PostMapping("/edit")
	public ResMsg edit(Purchase purchase){
        try{
            purchaseService.updateById(purchase);
            return ResMsg.ok();
        }catch(Exception e){
            log.error("更新采购表失败", e);
            return ResMsg.error("更新采购表失败");
        }

	}

	@Log("删除采购表")
	@PostMapping( "/remove")
	@ResponseBody
	public ResMsg remove(@RequestParam("id") Long id){
        try{
            purchaseService.deleteById(id);
            return ResMsg.ok();
        }catch(Exception e){
            return ResMsg.error();
        }
	}

	@Log("批量删除采购表")
	@PostMapping( "/batchRemove")
	@ResponseBody
	public ResMsg batchRemove(@RequestParam("ids[]") Long[] ids){
        purchaseService.deleteBatchIds(Arrays.asList(ids));
		return ResMsg.ok();
	}

}

